/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package codegener.comp;

import codegener.entity.DsBean;
import codegener.util.JsonUtils;
import codegener.util.StrUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author 32181
 */
public class DsForm extends javax.swing.JPanel {

    List<DsBean> dss=new ArrayList<>();
    /**
     * Creates new form dsForm
     */
    public DsForm() {
        initComponents();
        File conf=new File("./conf/dss.json");
        if(conf.exists()){
            try{
                String content=FileUtils.readFileToString(conf, "UTF-8"); 
                if(StrUtils.isNotBlank(content)){
                    List<Map<String,Object>> datas=JsonUtils.parseJsonToMaps(content);
                    if(null!=datas&&!datas.isEmpty()){
                        for(Map<String,Object> data:datas){
                            DsBean dsBean=new DsBean();
                            dsBean.setType(String.valueOf(data.get("type")));
                            dsBean.setUname(String.valueOf(data.get("uname")));
                            dsBean.setUpass(String.valueOf(data.get("upass")));
                            dsBean.setUrl(String.valueOf(data.get("url")));
                            dsBean.setSelected(Boolean.valueOf(String.valueOf(data.get("selected"))));
                            dss.add(dsBean);
                        }
                    }
                }
                
                initTxt();
            }catch(Exception e){
                e.printStackTrace();
            }
        }else{
            System.out.println("dss.json文件不存在："+conf.getAbsolutePath());
        }
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        dbType = new javax.swing.JComboBox<>();
        jLabel2 = new javax.swing.JLabel();
        urlTxt = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        unameTxt = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        upassTxt = new javax.swing.JTextField();
        submitBtn = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        confs = new javax.swing.JComboBox<>();

        jLabel1.setText("数据库类型：");

        dbType.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "MySQL", "Oracle", "达梦" }));
        dbType.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                dbTypeItemStateChanged(evt);
            }
        });

        jLabel2.setText("URL：");

        jLabel3.setText("账 号：");

        jLabel4.setText("密 码：");

        submitBtn.setText("保  存");
        submitBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitBtnActionPerformed(evt);
            }
        });

        jLabel6.setText("选择配置：");

        confs.setEditable(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(submitBtn)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(urlTxt)
                            .addComponent(dbType, 0, 250, Short.MAX_VALUE)
                            .addComponent(unameTxt)
                            .addComponent(upassTxt)
                            .addComponent(confs, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(104, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(86, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(confs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(dbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(urlTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(unameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(upassTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47)
                .addComponent(submitBtn)
                .addGap(37, 37, 37))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void submitBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitBtnActionPerformed
        // TODO add your handling code here:
        String selectedTypeString=dbType.getSelectedItem().toString();
        DsBean dsBean=getDsBean(selectedTypeString);
        boolean needAdd=false;
        if(null==dsBean){
            dsBean=new DsBean();
            needAdd=true;
        }
        
        dsBean.setType(dbType.getSelectedItem().toString());
        dsBean.setUname(unameTxt.getText().trim());
        dsBean.setUpass(upassTxt.getText().trim());
        dsBean.setUrl(urlTxt.getText().trim());
        if(needAdd){
            dss.add(dsBean);    
        }
        
        File confDir=new File("./conf");
        
        if(!confDir.exists()){
            confDir.mkdir();
        }
        try{
            for(DsBean ds:dss){
                ds.setSelected(false);
                if(selectedTypeString.equalsIgnoreCase(ds.getType())){
                    ds.setSelected(true);
                }
            }
            FileUtils.writeStringToFile(new File("./conf/dss.json"),JsonUtils.toJsonStr(dss), "UTF-8");
        }catch(Exception e){
            e.printStackTrace();
        }
    }//GEN-LAST:event_submitBtnActionPerformed
    
    private DsBean getDsBean(String type){
         for(DsBean ds:dss){
                if(ds.getType().equals(type)){
                    return ds;
                }
         }
         return null;
    }
    
    public DsBean getNowDsBean(){
        String type=dbType.getSelectedItem().toString();
        return getDsBean(type);
    }
   
    
    private void initTxt(){
        if(dss.size()>0){
            for(DsBean ds:dss){
                if(ds.isSelected()){
                    urlTxt.setText(ds.getUrl());
                    unameTxt.setText(ds.getUname());
                    upassTxt.setText(ds.getUpass());
                    dbType.setSelectedItem(ds.getType());
                }
            }
        }
    }
    
    
    
    private void dbTypeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_dbTypeItemStateChanged
        // TODO add your handling code here:
        
       
    }//GEN-LAST:event_dbTypeItemStateChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox<String> confs;
    private javax.swing.JComboBox<String> dbType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JButton submitBtn;
    private javax.swing.JTextField unameTxt;
    private javax.swing.JTextField upassTxt;
    private javax.swing.JTextField urlTxt;
    // End of variables declaration//GEN-END:variables
}
